package com.chuangsi.project.admin.controller.admin;

import com.chuangsi.framework.web.controller.BaseController;
import com.chuangsi.framework.web.domain.ResultObject;
import com.chuangsi.framework.web.page.TableDataInfo;
import com.chuangsi.project.admin.dto.admin.agreement.*;
import com.chuangsi.project.admin.dto.common.SelectDto;
import com.chuangsi.project.admin.service.AgreementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangshengming
 * @date 2023/3/24
 */
@Api(tags = "合同模板管理")
@RestController
@RequestMapping("/adminApi/agreement")
public class AgreementController extends BaseController {

    @Autowired
    private AgreementService agreementService;

    @ApiOperation(value = "合同模板分页",notes = "王圣铭")
    @GetMapping("/pageQuery")
    public TableDataInfo<ResAgreementPageDto> pageQuery(ReqAgreementPageDto req){
        startPage();
        List<ResAgreementPageDto> page = agreementService.pageQuery(req);
        return getDataTable(page);
    }

    @ApiOperation(value = "新增合同模板",notes = "王圣铭")
    @PostMapping("/addAgreement")
    public ResultObject<Void> addAgreement(@RequestBody @Valid ReqAgreementAddDto req){
        boolean flag = agreementService.addAgreement(req);
        if(flag){return ResultObject.ok("新增成功");}
        else {return ResultObject.error("新增失败");}
    }

    @ApiOperation(value = "修改合同模板",notes = "王圣铭")
    @PostMapping("/updateAgreement")
    public ResultObject<Void> updateAgreement(@RequestBody @Valid ReqAgreementUpdateDto req){
        boolean flag = agreementService.updateAgreement(req);
        if(flag){return ResultObject.ok("修改成功");}
        else {return ResultObject.error("修改失败");}
    }

    @ApiOperation(value = "合同模板详情",notes = "王圣铭")
    @PostMapping("/agreementDetail")
    public ResultObject<ResAgreementDetailDto> agreementDetail(@RequestBody @Valid ReqAgreementDetailDto req){
        ResAgreementDetailDto res = agreementService.agreementDetail(req);
        return ResultObject.ok("查询成功").setData(res);
    }

    @ApiOperation(value = "删除合同模板",notes = "王圣铭")
    @PostMapping("/agreementDel")
    public ResultObject<Void> agreementDel(@RequestBody @Valid ReqAgreementDetailDto req){
        agreementService.removeById(req.getAgreementId());
        return ResultObject.ok("删除成功");
    }

    @ApiOperation(value = "合同审批通过",notes = "王圣铭")
    @PostMapping("/pass")
    public ResultObject<Void> pass(@RequestBody @Valid ReqAgreementPassDto req){
        boolean flag = agreementService.pass(req);
        if(flag){return ResultObject.ok("操作成功");}
        else {return ResultObject.error("操作失败");}
    }

    @ApiOperation(value = "合同下拉选",notes = "王圣铭")
    @PostMapping("/agreementSelect")
    public ResultObject<List<SelectDto>> agreementSelect(){
        List<SelectDto> list = agreementService.agreementSelect();
        return ResultObject.ok("查询成功").setData(list);
    }


}
